﻿import numpy as np
import matplotlib.pyplot as plt

np.random.seed(42)

# 数据
X = 2*np.random.rand(100,1)
y = 4+3*X+np.random.randn(100,1)
# 增加一列，用于求解偏置项b
X_b = np.c_[(np.ones((100,1)), X)]


def learning_schedule(t):
    return 5/(50+t)


theta_path_mgd = []
m = len(X_b)
n_epochs = 50
minibatch = 16
np.random.seed(0)
theta = np.random.randn(2, 1)
t = 0
for epoch in range(n_epochs):
    shuffled_indices = np.random.permutation(m)
    X_b_shuffled = X_b[shuffled_indices]
    y_shuffled = y[shuffled_indices]
    for i in range(0, m, minibatch):
        t += 1
        xi = X_b_shuffled[i:i+minibatch]
        yi = y_shuffled[i:i+minibatch]
        gradients = 2/minibatch*xi.T.dot(xi.dot(theta) - yi)
        eta = learning_schedule(t)
        theta = theta-eta*gradients
        theta_path_mgd.append(theta)

print(theta) # [[4.20633761], [2.77808333]]